package com.stlm2.core.redis.queue;

import com.stlm2.core.redis.RedisClient;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * 出队操作

 */
public class Poper {

    /**
     * 出队后的消息处理器
     */
    private final Handler handler;

    public Poper(Handler handler){
        this.handler = handler;
    }

    /**
     * 出队
     * @param queue
     */
    public void pop(final String queue){
        final Jedis jedis = RedisClient.getResource();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    List<String> messages = jedis.brpop(Integer.MAX_VALUE, queue);
                    if(messages != null && messages.size() > 0){
                        if(queue.equals(messages.get(0)))
                            handler.handle(messages.get(1));
                    }
                }
            }
        });
        thread.start();
    }
}
